import axios from 'axios'
const baseURL = process.env.NODE_ENV === 'development' ? '/api' : '';/*ip和端口*/
const SUCCESS_CODE = 200;
const DEFAULT_SCORE = 50;/*分数越高线越高，以50分为基本分，最低分为50分*/
const DEFAULT_COLOR = ['#2B2B2B', 'green', 'orange', 'yellow', '#2B2B2B'];
const MAX_SCORE = 200;/*最高分为200分*/
const PHOTO_CODE = "id_photo";
const DATA_COOE = "id_data";
const BIAO_COOE = "id_relationship";
const SUPER_FILE_COOE = "sup_doc";
const REPLACE_COOD = '_idno_';
const REPLACE_COOD_BIAO = 'c__idno_';
const DEFAULT_IMG = '';
const BIAO_ZHI_HAO_IMG = '';
const CESHI_TUPIAN = 'http://68.202.4.5:8181/statics/img/default.805afcc.jpg';
const WAI_LIAN_TUPIAN = 'http://img4.imgtn.bdimg.com/it/u=992586811,744845853&fm=26&gp=0.jpg';
let photoUrl = '';
let dataUrl = '';
let biaoUrl = '';
let superFileUrl = '';
let hasRela = {};/*查找关系的已有数据，用来过滤已有数据*/
let hasDatas = {};/*标志号已经有过的数据，留作筛选*/
export const apiGetConfig = (ev, val) => {/*获取配置*/
	axios.get(baseURL + '/relation/config', { headers: { token: sessionStorage.getItem('token') } }).then((res) => {
		if (res.status == SUCCESS_CODE) {
			// console.log(res);
			res.data.map(item => {
				if (item.code == PHOTO_CODE) { photoUrl = item.url }
				if (item.code == DATA_COOE) { dataUrl = item.url }
				if (item.code == BIAO_COOE) { biaoUrl = item.url }
				if (item.code == SUPER_FILE_COOE) { superFileUrl = item.url }
			})
			ev.showNav = true
		} else {
			console.log("获取配置错误")
		}
		console.log("获取配置结束")
	})
}

export const apiShowDialog = (ev, val) => {/*标志号的查看详情*/
	return axios.post(baseURL + '/detail', { "v": val }, { headers: { token: sessionStorage.getItem('token') } }).then(res => {/*点击进入详情*/
		if (res.data.reqCode == SUCCESS_CODE) {
			let dataDiy = [];
			ev.dialogUserInfo = res.data.data;
			//		ev.dialogUserInfo = dataDiy;
			ev.isShowInfo = false;
			ev.dialogTableVisible_biaozhihao = true
		} else {

		}
	})
}

function biaozhihao(ev, val) {/*弹出标志号的信息*/
	//	console.log(ev);
	let biaoshi_name = '标志号$weiyi_123456789';
	pushDataAndLink(ev, biaoshi_name, 80, 0, false, 0, BIAO_ZHI_HAO_IMG);
	let NewbiaoUrl = biaoUrl.indexOf(REPLACE_COOD_BIAO) != -1 ? biaoUrl.replace(new RegExp(REPLACE_COOD_BIAO, 'g'), 'c_' + val) : biaoUrl;
	axios.post(baseURL + '/relation/forward?url=' + NewbiaoUrl, { headers: { token: sessionStorage.getItem('token') } }).then((res) => {
		//		console.log(res);
		if (res.data.reqCode == SUCCESS_CODE) {
			hasDatas = res.data.data;
			//			console.log(hasDatas)
			for (let obj in res.data.data) {
				for (let o in res.data.data[obj]) {
					for (let i in res.data.data[obj][o]) {
						//						console.log(res.data.data[obj][o][i])
						ev.option2.series[0].data.push(/*item的push*/
							{
								name: i,
								touchId: res.data.data[obj][o][i],
								fatherName: obj,
								"symbolSize": [180, 40],
								draggable: true,
								label: {
									"normal": {
										"textStyle": {
											color: 'blue',
										},
										position: '',
									}
								},
								itemStyle: {
									normal: {
										color: "#fff",
										shadowColor: 'rgba(0, 0, 0, 0.1)',
										shadowBlur: 50,

									}
								},
							},
						);
						ev.option2.series[0].links.push(/*links的push*/
							{
								source: biaoshi_name,
								target: String(i),
								lineStyle: {
									normal: {
										color: '#2B2B2B',
									}
								}
							},
						)
					}
				}
			}
		} else {
			console.log('标志号查询出错')
		}
	})
}

export const apiGetInfo = (ev, val) => {/*第一次查身份证*/
	ev.isShowInfo = false;
	ev.echarts_index = false;
	ev.contextmenu_div = false;
	ev.personInfo = [];
	ev.option2.series[0].data = [];
	ev.option2.series[0].links = [];
	let userName = '';
	let replaceName = '';
	let userImg = '';
	let firstName = val;
	userImg = getItemInfo(ev, val, 'img');

	//			console.log("获取第一个人的图片,名字");
	getItemName(ev, val).then(res_first => {
						// console.log(res_first)
		res_first.data = typeof (res_first.data) == 'string' ? eval('(' + res_first.data + ')') : res_first.data;
		if (res_first.data.success != true || res_first.data.data.length == 0) {
			replaceName = val
		} else {
			replaceName = res_first.data.data[0].xm
		}

	}).catch(error => { console.log(error) })


	axios.get(baseURL + '/relation/' + val, { headers: { token: sessionStorage.getItem('token') } }).then((res) => {
		//   	ev.emitRes(res);
		//   	console.log("第一次查身份证")
		if (res.status == SUCCESS_CODE) {
			/*第一个显示s*/

			ev.option2.series[0].data.push(
				{
					name: firstName + '$weiyi_' + val,
					"symbol": "image://" + DEFAULT_IMG,
					"userImg": "image://" + userImg,
					value: 0,
					x: ev.index_resize.width / 2,
					y: ev.index_resize.height / 2,
					fixed: true,
					category: 0,
					"symbolSize": 100,
					zjhm: val,
					flag: res.data[val].flag
				}
			)

			//				console.log(res);

			if (res.data.length == 0 || res.data == null) { ev.echarts_index = true; return }
			biaozhihao(ev, val);/*标志号*/
			let i = 0;
			res.data.result.map(item => {
				pushDataAndLink(ev, item.label, 80, 0, false, 0, item.img)/*第一层*/
				item.relations.map(item_relations => {
					pushDataAndLink(ev, item_relations.label, 60, item.label, false, 0, item_relations.img)/*第二层*/
					item_relations.datas.map(item_datas => {
						/*同步姓名，第二版本，后端会发来名字，这里同步照片*/
						//								getItemName(ev,item_datas.zjhm).then(res=>{
						i++;
						//									res.data = typeof(res.data)=='string'?eval('(' + res.data + ')'):res.data;
						//									
						//									if(res.data.data.length!=0&&res.data.data.length!=null){
						//										userName =  res.data.data[0].xm+'$weiyi_'+item_datas.zjhm+'$id='+item.id+'$code='+item_relations.code
						//										userImg =getItemInfo(ev,item_datas.zjhm,'img');
						//									    pushDataAndLink(ev,userName,30,item_relations.label,false,item_datas.zjhm,userImg,item_datas.time)/*第三层*/
						//									}else{
						//										pushDataAndLink(ev,item_datas.zjhm+'$weiyi_'+item_datas.zjhm+'$id='+item.id+'$code='+item_relations.code,30,item_relations.label,false,item_datas.zjhm,DEFAULT_IMG,item_datas.time)/*第三层*/
						//									}

						//								}).catch(error=>{
						//									console.log(error)
						//								})
						userImg = getItemInfo(ev, item_datas.zjhm, 'img');
						if (item_datas.hasOwnProperty('xm') && item_datas.xm != null && item_datas.xm != '') {
							userName = item_datas.xm + '$weiyi_' + item_datas.zjhm + '$id=' + item.id + '$code=' + item_relations.code;
						} else {
							userName = item_datas.zjhm + '$weiyi_' + item_datas.zjhm + '$id=' + item.id + '$code=' + item_relations.code;
						}
						pushDataAndLink(ev, userName, 30, item_relations.label, false, item_datas.zjhm, userImg, item_datas.time, item_relations.code,
							item_datas.ids, item_relations.score, item_relations.label, Number(item_datas.time) * Number(item_relations.score), item_datas.time, item_datas.flag)/*第三层*/
						//								if(i>=item_relations.datas.length){/*解决性能问题，只有大于才去执行这个方法*/
						//									ev.finish();	
						//								}

						/*同步姓名*/
					})
				})
			})
			if (replaceName != '') {
				ev.option2.series[0].data[0].name = replaceName + '$weiyi_' + val;
				ev.option2.series[0].links.map(item_replace2 => {
					item_replace2
					if (item_replace2.source == val + '$weiyi_' + val) {
						item_replace2.source = replaceName + '$weiyi_' + val;
					}
				})
			}
			ev.finish();
			ev.echarts_index = true;
		} else {
			console.log("第一次查身份证错误")
		}
	})
}

/*
 * ev:this,
 * data_name:节点显示的名称，线的目标
 * symbolSize：图片的大小，
 * source_name：线的源头，
 * fixed_true：是否固定,
 * zjhm:身份证，
 * userImg：用户图片,
 * time:次数*分数，字符串
 * code：后台需要
 * ids:后台需要，
 * score：前台显示，分数，
 * label：前台显示：关系名称,
 * allScore：总分，数字，打开详情需要
 * itemTime:每个关系的分数，数字，打开详情需要
 * flag:查询是否那个重点人员
 */
function pushDataAndLink(ev, data_name, symbolSize, source_name, fixed_true, zjhm = 0,
	userImg = DEFAULT_IMG, time = 0, code = 0, ids = 0, score = 0, label = "", allScore = 0, itemTime = 0, flag = 0) {/*push进去option2，抽离的方法*/
	ev.option2.series[0].data.push(
		{
			name: data_name,
			"symbol": "image://" + DEFAULT_IMG,
			userImg: "image://" + userImg,
			value: 0,
			category: 0,
			fixed: fixed_true,
			"symbolSize": symbolSize,
			zjhm: zjhm,
			flag: flag,
			//					itemStyle: {
			//	                            normal: {
			//	                                borderColor: 'red',
			//	                                borderWidth: 4,
			//	                                shadowBlur: 10,
			//	                                shadowColor: 'red',
			//	                                color: 'red',
			//	                            }
			//	                },
		}
	);
	ev.option2.series[0].links.push(
		{
			source: source_name,
			target: data_name,
			value: time,
			code: code,
			ids: ids,
			score: score,
			label: label,
			allScore: allScore,
			itemTime: itemTime,
			zjhm: zjhm,
			lineStyle: {
				normal: {
					color: allScore > MAX_SCORE ? DEFAULT_COLOR[1] : allScore < DEFAULT_SCORE ? DEFAULT_COLOR[4] : DEFAULT_COLOR[parseInt(allScore / DEFAULT_SCORE)]
				}
			}
		},
	);
}

function getItemInfo(ev, zjhm, type) {/*根据身份证获取人的信息*/
	if (type == 'img') {
		//		console.log(photoUrl);
		return photoUrl.indexOf(REPLACE_COOD) != -1 ? photoUrl.replace(new RegExp(REPLACE_COOD, 'g'), zjhm.trim()) : photoUrl;/*用户图片*/
	} else if (type == 'name') {
		//		console.log(dataUrl);
	}
}

function getItemName(ev, zjhm) {/*同步*/
	return new Promise((resolve, reject) => {
		let NewdataUrl = dataUrl.indexOf(REPLACE_COOD) != -1 ? dataUrl.replace(new RegExp(REPLACE_COOD, 'g'), zjhm) : dataUrl;
		axios.get(baseURL + '/relation/forward?url=' + NewdataUrl, { headers: { token: sessionStorage.getItem('token') } }).then(res => {
			res.data = typeof (res.data) == 'string' ? eval('(' + res.data + ')') : res.data;
			if (res.status == SUCCESS_CODE) {
				resolve(res);
			} else {
				//			 		console.log("同步信息出错")
			}
		}).catch(error => {
			reject(error);
		})
	})
}

function isHasImg(pathImg) {/*验证图片是否存在*/
	var ImgObj = new Image();
	ImgObj.src = pathImg;
	if (ImgObj.fileSize > 0 || (ImgObj.width > 0 && ImgObj.height > 0)) {
		return true;
	} else {
		return false;
	}
}

function getAge(status) {/*计算年龄*/
	// var aDate = new Date();
	// var thisYear = aDate.getFullYear();
		// console.log(status, thisYear)
	// var brith = status.toString().substring(0, 4)
	// var age = (thisYear - brith);
	let age = new Date().getFullYear() - new Date(parseInt(status)).getFullYear() + '岁'
	return age;
}

export const apiClickGetInfo = (ev, val) => {/*左键*/
	//	console.log(val.indexOf('$weiyi_'));
	//	console.log(val.substring(0,val.indexOf("$weiyi_")));
	let sub_val = val.indexOf('$weiyi') == -1 ? val : val.substring(0, val.indexOf("$weiyi_"))
	//	console.log(sub_val)
	axios.get(baseURL + '/relation/' + sub_val).then((res) => {
		if (res.status == SUCCESS_CODE) {
			pushItem(ev, res, val);
		}
	})
}

export const apiShowInfo = (ev, val) => {/*弹出人的信息*/
	//	console.log(val);
	let sfzName = val.data.zjhm.indexOf("$weiyi_") != -1 ? val.data.zjhm.substring(0, val.data.zjhm.indexOf("$weiyi_")) : val.data.zjhm
	let NewdataUrl = dataUrl.indexOf(REPLACE_COOD) != -1 ? dataUrl.replace(new RegExp(REPLACE_COOD, 'g'), sfzName) : dataUrl;

	axios.get(baseURL + '/relation/forward?url=' + NewdataUrl, { headers: { token: sessionStorage.getItem('token') } }).then(res => {
		//	 	console.log(res);
		//	 	console.log(res.data);

		res.data = typeof (res.data) == 'string' ? eval('(' + res.data + ')') : res.data;
		if (res.status == SUCCESS_CODE) {
			let xinxi = {};
			//	 		console.log(res)
			if (!res.data.success || res.data.data.length == 0) {
				xinxi.sfzh = sfzName;
				xinxi.xm = val.data.name.indexOf("$weiyi_") != -1 ? val.data.name.substring(0, val.data.name.indexOf("$weiyi_")) : val.data.name
			} else {
				xinxi = res.data.data[0];
				xinxi.csrq = getAge(xinxi.csrq);
			}
			let user_img = photoUrl.indexOf(REPLACE_COOD) != -1 ? photoUrl.replace(new RegExp(REPLACE_COOD, 'g'), sfzName) : photoUrl;
			xinxi.img_url = user_img
			//	 		console.log("弹出人的信息");
			ev.personInfo.push(xinxi);
			setTimeout(() => {/*解决vue的先后顺序执行问题*/
				ev.dragFunc('idNum_' + xinxi.sfzh, ev.personInfo.length - 1)
			}, 500)
		} else {
			//	 		console.log("弹出人的信息错误")
		}
	})
}

export const apiShowRelation = (ev, val) => {/*查看人的拓展关系*/
	//	console.log(val);
	let sfzNum = val.data.zjhm.indexOf('$weiyi') != -1 ? val.data.zjhm.substring(0, val.data.zjhm.indexOf('$weiyi')) : val.data.zjhm
	//	console.log(sfzNum);
	axios.get(baseURL + '/relation/' + sfzNum, { headers: { token: sessionStorage.getItem('token') } }).then(res => {
		let userName = '';
		let userImg = '';
		let ImgObj = new Image();
		//		console.log("查看人的关系，图片及其名字");
		//		console.log(res)
		let i = 0;
		/*循环开始*/
		res.data.result.map(item => {
			item.relations.map(item_rela => {
				item_rela.datas.map(item_datas => {
					/*同步姓名*/
					//					getItemName(ev,item_datas.zjhm).then(res=>{
					i++;
					//						res.data = typeof(res.data)=='string'?eval('(' + res.data + ')'):res.data;
					userImg = getItemInfo(ev, item_datas.zjhm, 'img');
					if (item_datas.hasOwnProperty('xm') && item_datas.xm != null && item_datas.xm != '') {
						userName = item_datas.xm

						//								if(!IsExist(userImg)){userImg=DEFAULT_IMG}
						//								console.log(userImg)
						if (item_datas.zjhm != sfzNum) {/*排除自己*/
							//											console.log("插入数据")
							//											console.log('长度为：'+item_rela.datas.length);
							//											console.log(filter(ev,userName+'$weiyi_'+item_datas.zjhm))/*有就返回false，无就返回true*/
							if (filter(ev, userName + '$weiyi_' + item_datas.zjhm)) {/*过滤已经有的*/
								ev.option2.series[0].data.push(
									{
										name: userName + '$weiyi_' + item_datas.zjhm,
										"symbol": "image://" + DEFAULT_IMG,
										userImg: "image://" + userImg,
										value: 0,
										category: 0,
										fixed: false,
										"symbolSize": 30,
										zjhm: item_datas.zjhm,
										flag: item_datas.flag
									}
								)
								hasRela[userName + '$weiyi_' + item_datas.zjhm] = 1;
							} else {
								ev.option2.series[0].links.map(item_link => {
									if (item_link.target == userName + '$weiyi_' + item_datas.zjhm && item_link.source == val.data.name) {/*要指定目标及来源的线*/
										//										   				console.log(item_link)
										item_link.value = item_link.value + ',' + Number(item_datas.time) * Number(item_rela.score) + '分' + item_rela.label;
										item_link.code = item_link.code + ',' + item_rela.code;
										item_link.label = item_link.label + ',' + item_rela.label;
										item_link.score = item_link.score + ',' + item_rela.score;
										item_link.ids = item_link.ids + '$weiyiids_' + item_datas.ids;
										item_link.allScore = item_link.allScore + Number(item_datas.time) * Number(item_rela.score)
										item_link.itemTime = item_link.itemTime + ',' + item_datas.time

									}
								})
							}
							let isPushLink = true;
							ev.option2.series[0].links.map(item_link => {/*筛选出是否已经存在目标和源头刚好相反的线*/
								if (item_link.target == val.data.name && item_link.source == userName + '$weiyi_' + item_datas.zjhm) {/*找到这条线*/
									isPushLink = false;
								}
							})
							if (isPushLink) {
								ev.option2.series[0].links.push(
									{
										source: val.data.name,
										value: Number(item_datas.time) * Number(item_rela.score) + '分' + item_rela.label,
										target: userName + '$weiyi_' + item_datas.zjhm,
										code: item_rela.code,
										ids: item_datas.ids,
										score: item_rela.score,
										label: item_rela.label,
										allScore: Number(item_datas.time) * Number(item_rela.score),
										itemTime: item_datas.time,
										lineStyle: {
											normal: {
												//													             width: Number(item_datas.time)*Number(item_rela.score)>MAX_SCORE?MAX_SCORE/DEFAULT_SCORE:Number(item_datas.time)*Number(item_rela.score)<DEFAULT_SCORE?DEFAULT_SCORE/DEFAULT_SCORE:Number(item_datas.time)*Number(item_rela.score)/DEFAULT_SCORE,
												color: Number(item_datas.time) * Number(item_rela.score) > MAX_SCORE ? DEFAULT_COLOR[1] : Number(item_datas.time) * Number(item_rela.score) < DEFAULT_SCORE ? DEFAULT_COLOR[4] : DEFAULT_COLOR[parseInt(Number(item_datas.time) * Number(item_rela.score) / DEFAULT_SCORE)]
											}
										}
									},
								)
							}

						}
					} else {
						console.log("%c获取不了数据", "color:red");
						//							console.log(res);
						if (filter(ev, item_datas.zjhm + '$weiyi_' + item_datas.zjhm)) {/*查不了数据也要过滤自己*/
							ev.option2.series[0].data.push(
								{
									name: item_datas.zjhm + '$weiyi_' + item_datas.zjhm,
									"symbol": "image://" + userImg,
									"userImg": "image://" + DEFAULT_IMG,
									value: 0,
									category: 0,
									fixed: false,
									"symbolSize": 30,
									zjhm: item_datas.zjhm,
									flag: item_datas.flag,
									//												itemStyle: {
									//								                            normal: {
									//								                                borderColor: 'blue',
									//								                                borderWidth: 4,
									//								                                shadowBlur: 10,
									//								                                shadowColor: 'blue',
									//								                                color: 'blue',
									//								                            }
									//								                },
								}
							)
							hasRela[item_datas.zjhm + '$weiyi_' + item_datas.zjhm] = 1;
						} else {
							ev.option2.series[0].links.map(item_link => {
								if (item_link.target == item_datas.zjhm + '$weiyi_' + item_datas.zjhm && item_link.source == val.data.name) {/*要指定目标及来源的线*/
									//										   				console.log(item_link)
									item_link.value = item_link.value + ',' + Number(item_datas.time) * Number(item_rela.score) + '分' + item_rela.label;
									item_link.code = item_link.code + ',' + item_rela.code;
									item_link.label = item_link.label + ',' + item_rela.label;
									item_link.score = item_link.score + ',' + item_rela.score;
									item_link.ids = item_link.ids + '$weiyiids_' + item_datas.ids;
									item_link.allScore = item_link.allScore + Number(item_datas.time) * Number(item_rela.score)
									item_link.itemTime = item_link.itemTime + ',' + item_datas.time

								}
							})
						}

						let isPushLink = true;
						ev.option2.series[0].links.map(item_link => {/*筛选出是否已经存在目标和源头刚好相反的线*/
							if (item_link.target == val.data.name && item_link.source == item_datas.zjhm + '$weiyi_' + item_datas.zjhm) {/*找到这条线*/
								isPushLink = false;
							}
						})
						if (isPushLink) {
							ev.option2.series[0].links.push(
								{
									source: val.data.name,
									value: Number(item_datas.time) * Number(item_rela.score) + '分' + item_rela.label,
									target: item_datas.zjhm + '$weiyi_' + item_datas.zjhm,
									code: item_rela.code,
									ids: item_datas.ids,
									score: item_rela.score,
									label: item_rela.label,
									allScore: Number(item_datas.time) * Number(item_rela.score),
									itemTime: item_datas.time,
									lineStyle: {
										normal: {
											//													            width: Number(item_datas.time)*Number(item_rela.score)>MAX_SCORE?MAX_SCORE/DEFAULT_SCORE:Number(item_datas.time)*Number(item_rela.score)<DEFAULT_SCORE?DEFAULT_SCORE/DEFAULT_SCORE:Number(item_datas.time)*Number(item_rela.score)/DEFAULT_SCORE,
											color: Number(item_datas.time) * Number(item_rela.score) > MAX_SCORE ? DEFAULT_COLOR[1] : Number(item_datas.time) * Number(item_rela.score) < DEFAULT_SCORE ? DEFAULT_COLOR[4] : DEFAULT_COLOR[parseInt(Number(item_datas.time) * Number(item_rela.score) / DEFAULT_SCORE)]
										}
									}
								},
							)
						}

					}
					//						if(i>=item_rela.datas.length){/*解决性能问题*/
					//							ev.finish();
					//						}
					//					})
				})
			})
		})
		ev.finish();
	})

}

function filter(ev, name) {/*过滤已经有的*/
	//	console.log(name);
	//	console.log(hasRela);
	//	console.log(hasRela[name]);
	if (hasRela[name] == undefined) {
		return true
	}
	return false;
}

export const apiSeachAnalyse = (ev, val, res) => {/*查找关系的第一次*/
	ev.contextmenu_div = false;
	ev.personInfo = [];
	ev.option2.series[0].data = [];
	ev.option2.series[0].links = [];
	hasRela = {};
	let userName = '';
	let userImg = '';
	let replaceName = '';
	let firstName = val;
	let ImgObj = new Image();
	userImg = getItemInfo(ev, val, 'img');
	getItemName(ev, val).then(res_first => {/*获取全国人口名字*/
		console.log('全国人口');
		res_first.data = typeof (res_first.data) == 'string' ? eval('(' + res_first.data + ')') : res_first.data;
		if (res_first.data.success != true || res_first.data.data.length == 0) {
			replaceName = val
			//					ev.option2.series[0].data.name = 'www'
		} else {
			replaceName = res_first.data.data[0].xm;
		}

	}).catch(error => { console.log(error) })
	axios.get(baseURL + '/relation/' + val, { headers: { token: sessionStorage.getItem('token') } }).then(res => {
		//		console.log('查找关系第一次')
		//		console.log(res);
		/*先清空数据*/
		//			userImg =  process.env.NODE_ENV === 'development' ? WAI_LIAN_TUPIAN : getItemInfo(ev,val,'img');
		//			if(isHasImg(userImg)){userImg=DEFAULT_IMG}
		/*第一个显示s*/
		//			获取第一个人的图片,名字
		//			console.log("获取第一个人的图片,名字");
		console.log('插入');
		ev.option2.series[0].data.push(
			{
				name: firstName + '$weiyi_' + val,
				"symbol": "image://" + DEFAULT_IMG,
				"userImg": "image://" + userImg,
				value: 0,
				x: ev.index_resize.width / 2,
				y: ev.index_resize.height / 2,
				fixed: true,
				category: 0,
				"symbolSize": 100,
				zjhm: val,
				flag: res.data[val].flag
			}
		)
		hasRela[firstName + '$weiyi_' + val] = 1;
		//				循环开始
		let i = 0;
		res.data.result.map(item => {
			item.relations.map(item_rela => {
				item_rela.datas.map(item_datas => {
					/*同步姓名*/
					//								getItemName(ev,item_datas.zjhm).then(res=>{
					i++;
					//									res.data = typeof(res.data)=='string'?eval('(' + res.data + ')'):res.data;
					//									userImg =getItemInfo(ev,item_datas.zjhm,'img');
					userImg = process.env.NODE_ENV === 'development' ? CESHI_TUPIAN : getItemInfo(ev, item_datas.zjhm, 'img');
					if (item_datas.hasOwnProperty('xm') && item_datas.xm != null && item_datas.xm != '') {
						userName = item_datas.xm

						if (filter(ev, userName + '$weiyi_' + item_datas.zjhm)) {
							pushDataAndLink(ev, userName + '$weiyi_' + item_datas.zjhm, 50, firstName + '$weiyi_' + val,
								false, item_datas.zjhm, userImg, Number(item_datas.time) * Number(item_rela.score) + '分' + item_rela.label, item_rela.code,
								item_datas.ids, item_rela.score, item_rela.label, Number(item_datas.time) * Number(item_rela.score), item_datas.time, item_datas.flag)/*第三层*/
							hasRela[userName + '$weiyi_' + item_datas.zjhm] = 1;
						} else {
							//									   		console.log(item_datas)
							ev.option2.series[0].links.map(item_link => {
								if (item_link.target == userName + '$weiyi_' + item_datas.zjhm && item_link.source == firstName + '$weiyi_' + val) {
									item_link.value = item_link.value + ',' + Number(item_datas.time) * Number(item_rela.score) + '分' + item_rela.label;
									item_link.code = item_link.code + ',' + item_rela.code;
									item_link.label = item_link.label + ',' + item_rela.label;
									item_link.score = item_link.score + ',' + item_rela.score;
									item_link.ids = item_link.ids + '$weiyiids_' + item_datas.ids;
									item_link.allScore = item_link.allScore + Number(item_datas.time) * Number(item_rela.score)
									item_link.itemTime = item_link.itemTime + ',' + item_datas.time
								}
							})
						}
					} else {/*查不了数据*/
						console.log('查不了数据');
						if (filter(ev, item_datas.zjhm + '$weiyi_' + item_datas.zjhm)) {/*过滤已经有的*/
							hasRela[item_datas.zjhm + '$weiyi_' + item_datas.zjhm] = 1;
							pushDataAndLink(ev, item_datas.zjhm + '$weiyi_' + item_datas.zjhm, 30, firstName + '$weiyi_' + val,
								false, item_datas.zjhm, userImg, Number(item_datas.time) * Number(item_rela.score) + '分' + item_rela.label, item_rela.code,
								item_datas.ids, item_rela.score, item_rela.label, Number(item_datas.time) * Number(item_rela.score), item_datas.time, item_datas.flag)/*第三层*/
						} else {
							ev.option2.series[0].links.map(item_link => {
								if (item_link.target == item_datas.zjhm + '$weiyi_' + item_datas.zjhm && item_link.source == firstName + '$weiyi_' + val) {
									item_link.value = item_link.value + ',' + Number(item_datas.time) * Number(item_rela.score) + '分' + item_rela.label;
									item_link.code = item_link.code + ',' + item_rela.code;
									item_link.label = item_link.label + ',' + item_rela.label;
									item_link.score = item_link.score + ',' + item_rela.score;
									item_link.ids = item_link.ids + '$weiyiids_' + item_datas.ids;
									item_link.allScore = item_link.allScore + Number(item_datas.time) * Number(item_rela.score)
									item_link.itemTime = item_link.itemTime + ',' + item_datas.time
								}
							})
						}

					}
					//									console.log(item_rela.datas.length)
					//									console.log(i);
					//									if(i>=item_rela.datas.length){/*解决过多的查询引发的性能问题*/
					//										ev.finish();	
					//									}

					//								}).catch(error=>{
					//									console.log(error)
					//								})
					/*同步姓名*/
				})
			})
		})
		if (replaceName != '') {
			console.log('有名字');
			ev.option2.series[0].data[0].name = replaceName + '$weiyi_' + val;
			hasRela[replaceName + '$weiyi_' + val] = 1;
			ev.option2.series[0].links.map(item_replace2 => {
				if (item_replace2.source == val + '$weiyi_' + val) {
					item_replace2.source = replaceName + '$weiyi_' + val;
				}
			})

		}
		ev.finish();/*修正图片*/
		ev.echart_analyse = true;
	})
}


async function IsExist(url)/*图片是否存在*/ {
	//		console.log("进入IsExist")
	await new Promise((resolve, reject) => {
		let img = new Image();
		img.src = url;
		img.addEventListener("load", () => {
			//					console.log("dwada");
			resolve(true);
		})
		img.addEventListener("error", () => {
			//					console.log("222");
			reject(false);
		})
	})
}

function PromiseImg(url) {/*暂时没用，后期用*/
	//	console.log("进入PromiseImg")
	return new Promise((resolve, reject) => {
		let img = new Image();
		img.addEventListener("load", () => {
			//					console.log("dwada");
			resolve(true);
		})
		img.addEventListener("error", () => {
			reject(false);
		})
		img.src = url;
	})
}


//定义抠图方法
/*
 * imgSrc:图片地址，
 * radius：半径
 * center：x,y,一般为0 0 
 * */
function getImgData(imgSrc, radius, center = {
	x: 0,
	y: 0
}) {
	return new Promise((resolve) => {
		const canvas = document.createElement('canvas');
		const contex = canvas.getContext('2d');
		const img = new Image();
		img.crossOrigin = '';
		const diameter = 2 * radius;
		img.onload = function () {
			canvas.width = diameter;
			canvas.height = diameter;
			contex.clearRect(0, 0, diameter, diameter);
			contex.save();
			contex.beginPath();
			contex.arc(radius, radius, radius, 0, 2 * Math.PI); //画出圆
			contex.clip(); //裁剪上面的圆形
			contex.drawImage(img, center.x - radius, center.y - radius, diameter, diameter, 0, 0, diameter, diameter); // 在刚刚裁剪的园上画图
			contex.restore(); // 还原状态
			resolve(canvas.toDataURL('image/png', 1))
		}
		img.src = imgSrc;
	})
}

//function axiosShowInfo(ev,i=0,code_array,ids_array,score_arr,label_arr,val){
//	if(i==code_array.length){
//		return;
//	}
//			axios({
//				method:'get',
//				url:baseURL+'/relation/data/'+code_array[i]+'?ids='+encodeURIComponent(ids_array[i]),
//			}).then(res=>{
//				ev.analyse.firstTable.firstName = val.data.source.indexOf('$weiyi_')==-1?val.data.source:val.data.source.substring(0,val.data.source.indexOf('$weiyi_'));
//				ev.analyse.firstTable.firstSFZ=val.data.source.indexOf('$weiyi_')==-1?val.data.source:val.data.source.substring(val.data.source.indexOf('$weiyi_')+7,val.data.source.length);
//				ev.analyse.firstTable.gxzs = val.data.allScore;
//				ev.analyse.firstTable.secName = val.data.target.indexOf('$weiyi_')==-1?val.data.target:val.data.target.substring(0,val.data.target.indexOf('$weiyi_'));
//	    		ev.analyse.firstTable.secSFZ = val.data.target.indexOf('$weiyi_')==-1?val.data.target:val.data.target.substring(val.data.target.indexOf('$weiyi_')+7,val.data.target.length);
//	    		ev.analyse.tableData1.push({rele_type:val.data.score_arr[i],rele_num:val.data.code_array[i],rele_name:val.data.label_arr[i]})
//	    		axiosShowInfo(ev,i+1,code_array,ids_array,score_arr,label_arr,val)
//			})
//	
//	
//}

/*关系分析*/
export const apiClickEdge = (ev, val) => {/*点击线,弹出详情*/
	//	console.log(val)
	let code_array = String(val.data.code).split(',')
	let ids_array = String(val.data.ids).split('$weiyiids_');
	let score_arr = String(val.data.score).split(',');
	let label_arr = String(val.data.label).split(',');
	let time_arr = String(val.data.itemTime).split(',');

	//	console.log(code_array);
	//	console.log(ids_array);
	ev.analyse.tableData1 = [];/*先清空*/
	ev.analyse.table3 = [];/*先清空*/
	//	axiosShowInfo(ev,0,code_array,ids_array,score_arr,label_arr,val);
	for (let i = 0; i < code_array.length; i++) {
		ev.analyse.firstTable.firstName = val.data.source.indexOf('$weiyi_') == -1 ? val.data.source : val.data.source.substring(0, val.data.source.indexOf('$weiyi_'));
		ev.analyse.firstTable.firstSFZ = val.data.source.indexOf('$weiyi_') == -1 ? val.data.source : val.data.source.substring(val.data.source.indexOf('$weiyi_') + 7, val.data.source.length);
		ev.analyse.firstTable.gxzs = val.data.allScore;
		ev.analyse.firstTable.secName = val.data.target.indexOf('$weiyi_') == -1 ? val.data.target : val.data.target.substring(0, val.data.target.indexOf('$weiyi_'));
		ev.analyse.firstTable.secSFZ = val.data.target.indexOf('$weiyi_') == -1 ? val.data.target : val.data.target.substring(val.data.target.indexOf('$weiyi_') + 7, val.data.target.length);
		ev.analyse.tableData1.push({ rele_type: score_arr[i], rele_num: time_arr[i], rele_name: label_arr[i] })

		axios({
			method: 'get',
			url: baseURL + '/relation/data/' + code_array[i] + '?ids=' + encodeURIComponent(ids_array[i]),
			headers: { token: sessionStorage.getItem('token') }
		}).then(res => {

			ev.analyse.table3.push(
				{
					name: label_arr[i],
					myself: val.data.source.indexOf('$weiyi_') == -1 ? val.data.source : val.data.source.substring(0, val.data.source.indexOf('$weiyi_')),
					tableLable3: res.data.header,
					tableData3: res.data.hasOwnProperty('result') && res.data.result.constructor === Array ? res.data.result : '',
				}
			)

		})
	}

}


/*关系查找*/
export const apiClickEdge2 = (ev, val) => {/*点击线,弹出详情*/
	//	console.log(val)
	let code_array = String(val.data.code).split(',')
	let ids_array = String(val.data.ids).split('$weiyiids_');
	let score_arr = String(val.data.score).split(',');
	let label_arr = String(val.data.label).split(',');
	let time_arr = String(val.data.itemTime).split(',');

	//	console.log(code_array);
	//	console.log(ids_array);
	ev.analyse.tableData1 = [];/*先清空*/
	ev.analyse.table3 = [];/*先清空*/

	//	axiosShowInfo(ev,0,code_array,ids_array,score_arr,label_arr,val);
	for (let i = 0; i < code_array.length; i++) {
		ev.analyse.tableData1.push({ rele_type: score_arr[i], rele_num: time_arr[i], rele_name: label_arr[i] })
		ev.analyse.firstTable.firstName = ev.option2.series[0].data[0].name.indexOf('$weiyi_') == -1 ? ev.option2.series[0].data[0].name : ev.option2.series[0].data[0].name.substring(0, ev.option2.series[0].data[0].name.indexOf('$weiyi_'));
		ev.analyse.firstTable.firstSFZ = ev.option2.series[0].data[0].name.indexOf('$weiyi_') == -1 ? ev.option2.series[0].data.name : ev.option2.series[0].data[0].name.substring(ev.option2.series[0].data[0].name.indexOf('$weiyi_') + 7, ev.option2.series[0].data[0].name.length);
		ev.analyse.firstTable.gxzs = val.data.allScore;
		ev.analyse.firstTable.secName = val.data.target.indexOf('$weiyi_') == -1 ? val.data.target : val.data.target.substring(0, val.data.target.indexOf('$weiyi_'));
		ev.analyse.firstTable.secSFZ = val.data.zjhm;
		axios({
			method: 'get',
			url: baseURL + '/relation/data/' + code_array[i] + '?ids=' + encodeURIComponent(ids_array[i]),
			headers: { token: sessionStorage.getItem('token') }
		}).then(res => {


			ev.analyse.table3.push(
				{
					name: label_arr[i],
					myself: val.data.source.indexOf('$weiyi_') == -1 ? val.data.source : val.data.source.substring(0, val.data.source.indexOf('$weiyi_')),
					tableLable3: res.data.header,
					tableData3: res.data.hasOwnProperty('result') && res.data.result.constructor === Array ? res.data.result : '',
				}
			)

		})
	}

}

function throwError(ev, msg) {/*抛出错误*/
	ev.$message({
		message: msg,
		type: "error",
		showClose: true,
	});
}

export const getURLinfo = (ev) => {/*获取url信息*/
	//		console.log(ev.$route);
	if (ev.$route.query.hasOwnProperty('info')) {
		//		console.log(ev.$route.query.info.split('_'))
		let arr1 = ev.$route.query.info.split('_');
		if (arr1.length == 2) {
			ev.form.input1 = String(arr1[1])
			ev.cliclSearchImg();
		}
		if (ev.$route.query.hasOwnProperty('token')) {
			ev.token = ev.$route.query.token
		}

	} else {
		return;
	}
}

export const getScoreVal = (ev) => {/*获取分值*/
	axios.get(baseURL + '/relations', { headers: { token: sessionStorage.getItem('token') } }).then(res => {
		//		console.log(res);
		ev.scoreVal = res.data
	})
}

export const apiSearchSFZ = (ev, form, item) => {/*搜索这个人和输入的身份证的关系*/
	//	console.log('配置ids');
	let ids = item.data.zjhm;
	let userImg = '';/*头像*/
	let userName = '';/*姓名*/
	let hasZJHM = [];
	for (let i = 0; i < form.length; i++) {
		ids += ',' + form[i];
		getItemName(ev, form[i]).then(res_first => {
			//			console.log(res_first)
			//			console.log('json格式可能会出错')
			res_first.data = typeof (res_first.data) == 'string' ? eval('(' + res_first.data + ')') : res_first.data;
			if (res_first.data.data.length == 0) { userName = form[i]/*没查到，等于身份证*/ } else { userName = res_first.data.data[0].xm;/*查到等于名字*/ }
			//			console.log('json格式没有出错')

			userImg = getItemInfo(ev, form[i], 'img');
			if (filter(ev, userName + '$weiyi_' + form[i])) {/*可以插入*/
				ev.option2.series[0].data.push(
					{
						name: userName + '$weiyi_' + form[i],
						"symbol": "image://" + DEFAULT_IMG,
						"userImg": "image://" + userImg,
						value: 0,
						x: 1125,
						y: 662,
						fixed: false,
						category: 0,
						"symbolSize": 60,
						zjhm: form[i],
						flag: 0
					}
				);
				hasRela[userName + '$weiyi_' + form[i]] = 1;
				ev.option2.series[0].links.push(
					{
						source: item.data.name,
						target: userName + '$weiyi_' + form[i],
						value: '无直接关系',
						code: 0,
						ids: 0,
						score: 0,
						label: '',
						allScore: 0,
						itemTime: 0,
						lineStyle: {
							normal: {
								color: 'red',
								width: 1,
							}
						}
					},
				);
				//			console.log("第一次插入")
			} else {/*不用插入，事前已经有了，把那条给换个颜色*/
				//				ev.option2.series[0].links.map(item_link=>{
				//					if(item_link.target== userName+'$weiyi_'+form[i]&&item_link.source==item.data.name){
				//						item_link.lineStyle={};
				//						item_link.lineStyle.normal={};
				////						console.log(item_link.lineStyle)
				//						item_link.lineStyle.normal.color = 'blue';
				//					}
				//				})
			}

			ev.finish();
			//			console.log(i,form.length)
			if (i == form.length - 1) { axiosIds(ev, ids); }/*最后一次，解决异步问题*/
		})
	}
}



function axiosIds(ev, ids) {/*分开写，解决异步问题*/
	axios.get(baseURL + '/relation/intersection?ids=' + ids, { headers: { token: sessionStorage.getItem('token') } }).then(res => {
		//		console.log(res);
		let now_item = '';/*当前这个身份证对应的这个ootion2的data*/
		let userImg = '';
		let userName = '';

		for (let k in res.data) { if (k != 'result') { ev.option2.series[0].data.map(item_option2_flag => { if (item_option2_flag.zjhm == k) { item_option2_flag.flag = res.data[k].flag } }) } }/*重点人员flag替代*/
		for (let i in res.data.result) {/*for循环里面的result，以result为准*/
			ev.option2.series[0].data.map(item_option2 => { if (item_option2.zjhm == i) { now_item = item_option2 } })/*找出当前这个人*/
			console.log(now_item);
			res.data.result[i].map(item => {
				item.relations.map(item_rela => {
					item_rela.datas.map(item_datas => {
						userImg = getItemInfo(ev, item_datas.zjhm, 'img');
						if (item_datas.hasOwnProperty('xm') && item_datas.xm != null && item_datas.xm != '') { userName = item_datas.xm } else { userName = item_datas.zjhm }
						if (filter(ev, userName + '$weiyi_' + item_datas.zjhm)) {/*可以插入*/
							ev.option2.series[0].data.push(
								{
									name: userName + '$weiyi_' + item_datas.zjhm,
									"symbol": "image://" + DEFAULT_IMG,
									"userImg": "image://" + userImg,
									value: 0,
									x: 1125,
									y: 662,
									fixed: false,
									category: 0,
									"symbolSize": 60,
									zjhm: item_datas.zjhm,
									flag: item_datas.flag,

								}
							);
							hasRela[userName + '$weiyi_' + item_datas.zjhm] = 1;
							//							console.log("第二次插入")
						} else {/*已经有数据,以result里面的为准*/
							ev.option2.series[0].data.map(item_option2_result => { if (item_option2_result.zjhm == item_datas.zjhm) { item_option2_result.flag = item_datas.flag } })
						}
						let isPushLink = true;
						ev.option2.series[0].links.map(item_link => {/*筛选出是否已经存在目标和源头刚好相反的线*/
							if (item_link.target == now_item.name && item_link.source == userName + '$weiyi_' + item_datas.zjhm) {/*找到这条线*/
								console.log('www')
								item_link.value = (item_link.value + ',' + Number(item_datas.time) * Number(item_rela.score) + '分' + item_rela.label).replace(/无直接关系,/, ''),
									item_link.code = item_link.code == 0 ? item_rela.code : item_link.code + ',' + item_rela.code,
									item_link.ids = item_link.ids == 0 ? item_datas.ids : item_link.ids + '$weiyiids_' + item_datas.ids,
									item_link.score = item_link.score == 0 ? item_rela.score : item_link.score + ',' + item_rela.score,
									item_link.label = item_link.label == '' ? item_rela.label : item_link.label + ',' + item_rela.label,
									item_link.allScore = item_link.allScore + Number(item_datas.time) * Number(item_rela.score),
									item_link.itemTime = item_link.itemTime == 0 ? item_datas.time : item_link.itemTime + ',' + item_datas.time,
									item_link.lineStyle = {
										normal: {
											color: item_link.allScore > MAX_SCORE ? DEFAULT_COLOR[1] : item_link.allScore < DEFAULT_SCORE ? DEFAULT_COLOR[4] : DEFAULT_COLOR[parseInt(item_link.allScore / DEFAULT_SCORE)],
											//						             							width: Number(item_datas.time)*Number(item_rela.score)>MAX_SCORE?MAX_SCORE/DEFAULT_SCORE:Number(item_datas.time)*Number(item_rela.score)/DEFAULT_SCORE,
										}
									}
								isPushLink = false;
							}
						})
						if (isPushLink) {
							console.log('？？？')
							ev.option2.series[0].links.push(
								{
									source: now_item.name,
									target: userName + '$weiyi_' + item_datas.zjhm,
									value: Number(item_datas.time) * Number(item_rela.score) + '分' + item_rela.label,
									code: item_rela.code,
									ids: item_datas.ids,
									score: item_rela.score,
									label: item_rela.label,
									allScore: Number(item_datas.time) * Number(item_rela.score),
									itemTime: item_datas.time,
									lineStyle: {
										normal: {
											color: Number(item_datas.time) * Number(item_rela.score) > MAX_SCORE ? DEFAULT_COLOR[1] : Number(item_datas.time) * Number(item_rela.score) < DEFAULT_SCORE ? DEFAULT_COLOR[4] : DEFAULT_COLOR[parseInt(Number(item_datas.time) * Number(item_rela.score) / DEFAULT_SCORE)],
											//						             								width: Number(item_datas.time)*Number(item_rela.score)>MAX_SCORE?MAX_SCORE/DEFAULT_SCORE:Number(item_datas.time)*Number(item_rela.score)/DEFAULT_SCORE,
										}
									}
								},
							);
						}
					})
				})
			})
		}




		ev.finish();
	})
}

export const gotoSuperFile = (ev, sfzm) => {/*跳转到超级档案*/
	if (sessionStorage.getItem('token')) {
		let token = sessionStorage.getItem('token')
		//		console.log(superFileUrl);
		//		console.log(token);
		let new_url = superFileUrl.replace('_token_', token)
		//		console.log(new_url)
		let new_url2 = new_url.replace('_idno_', sfzm)
		//		console.log(new_url2)
		window.open(new_url2, '_blank');
	} else {
		console.log(`没有token`);
	}
}


export const getWatermarka = (ev) => {
	axios.get(baseURL + '/user/info', { headers: { token: sessionStorage.getItem('token') } }).then(res => {
		if (res.status != 200) { console.log('获取出错userinfo'); return; }
		//		console.log(res)
		ev.watermarkStr = res.data.name + ' ' + res.data.cardNo + ' ' + getNowData();
		ev.watermarka()
	}).catch(error => {
		console.log(error)
	})
}

function getNowData() {
	let myDate = new Date();
	let year = myDate.getFullYear();
	let month = myDate.getMonth() + 1 < 10 ? '0' + (myDate.getMonth() + 1) : myDate.getMonth() + 1;
	let day = myDate.getDate() < 10 ? '0' + myDate.getDate() : myDate.getDate();
	return year + '-' + month + '-' + day
}
